import 'package:flutter/material.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';

///计算总数
class Calculator extends StatefulWidget {
  ///
  ValueChanged<int> count; //计算回调
  ///
  int sum; //初始数量

  ///
  Calculator({Key key, this.sum, this.count}) : super(key: key);

  @override
  _CalculatorState createState() => _CalculatorState();
}

class _CalculatorState extends State<Calculator> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              if (widget.sum > 0) {
                setState(() {
                  widget.sum -= 1;
                });
                widget.count(widget.sum);
              }
            },
            child: Container(
              padding: EdgeInsets.only(
                  left: setWidth(5),
                  right: setWidth(5),
                  top: setWidth(9),
                  bottom: setWidth(9)),
              child: Image.asset(
                "assets/images/num_minus.png",
                width: setWidth(9),
                height: setWidth(1),
                fit: BoxFit.cover,
              ),
              width: setWidth(20),
              height: setWidth(20),
            ),
          ),
          Container(
            padding: EdgeInsets.only(
                top: setWidth(4),
                bottom: setWidth(4),
                left: setWidth(18),
                right: setWidth(18)),
            decoration: BoxDecoration(
                color: Color(0xffF4F4F4),
                borderRadius: BorderRadius.circular(2)),
            child: Text("${widget.sum}"),
          ),
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              setState(() {
                widget.sum += 1;
              });
              widget.count(widget.sum);
            },
            child: Container(
              padding: EdgeInsets.all(setWidth(5)),
              child: Image.asset(
                "assets/images/num_add.png",
                width: setWidth(10),
                height: setWidth(10),
                fit: BoxFit.cover,
              ),
              width: setWidth(20),
              height: setWidth(20),
            ),
          ),
        ],
      ),
    );
  }
}
